package microassembler;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;

class assembler
{

    assembler(String s, String s1, symbolTable symboltable)
    {
        numErrors = 0;
        numWarnings = 0;
        theBranches = new branchcond();
        BufferedReader bufferedreader;
        try
        {
            bufferedreader = new BufferedReader(((java.io.Reader) (new FileReader(s))));
        }
        catch(Exception exception)
        {
            System.out.println("Error: Unable to open input file (" + s + ")");
            return;
        }
        PrintWriter printwriter;
        try
        {
            printwriter = new PrintWriter(((java.io.Writer) (new FileWriter(s1))));
        }
        catch(Exception exception1)
        {
            System.out.println("Error: Unable to open output file (" + s1 + ")");
            return;
        }
        try
        {
            int i = 0;
            int j = -1;
            String s2;
            while((s2 = bufferedreader.readLine()) != null) 
            {
                i++;
                s2 = s2.trim();
                if(!s2.startsWith("#") && s2.length() > 0)
                {
                    if(s2.startsWith("*"))
                    {
                        j = Integer.parseInt(s2.substring(1)) - 1;
                    } else
                    {
                        j++;
                        try
                        {
                            microinst microinst1 = new microinst(s2);
                            printwriter.println("*" + j + "\n" + getMachineInst(microinst1, symboltable));
                        }
                        catch(InvalidMicroInstException invalidmicroinstexception)
                        {
                            System.out.println("Major Bummer - this shouldn't happen." + globals.ENDL + "[ " + i + " ] Error: " + invalidmicroinstexception.cause);
                            numErrors++;
                        }
                        catch(SymbolTranslationException symboltranslationexception)
                        {
                            System.out.println("[ " + i + " ] Error: " + symboltranslationexception.cause);
                            numErrors++;
                        }
                    }
                }
            }
        }
        catch(IOException ioexception)
        {
            System.out.println(((Object) (ioexception)));
        }
        try
        {
            bufferedreader.close();
        }
        catch(IOException ioexception1)
        {
            System.out.println("Error: Unable to close input file (" + s + ")");
            numErrors++;
        }
        try
        {
            printwriter.close();
        }
        catch(Exception exception2)
        {
            System.out.println("Error: Unable to close output file (" + s1 + ")");
            numErrors++;
        }
    }

    private String getMachineInst(microinst microinst1, symbolTable symboltable)
        throws SymbolTranslationException
    {
        String s = (String)((Hashtable) (theBranches)).get("none");
        Integer integer = new Integer(0);
        if(microinst1.getBranchCondition() != null)
        {
            s = (String)((Hashtable) (theBranches)).get(((Object) (microinst1.getBranchCondition())));
            if(s == null)
            {
                throw new SymbolTranslationException("Unknown branch condition: " + microinst1.getBranchCondition() + ".");
            }
        }
        if(microinst1.getBranchLabel() != null)
        {
            integer = (Integer)((Hashtable) (symboltable)).get(((Object) (microinst1.getBranchLabel())));
            if(integer == null)
            {
                throw new SymbolTranslationException("Undefined branch label: " + microinst1.getBranchLabel() + ".");
            }
        }
        return microinst1.getMicroInst() + s + baseconv.dec2bin(integer.intValue(), 8);
    }

    public int getErrorCount()
    {
        return numErrors;
    }

    public int getWarningCount()
    {
        return numWarnings;
    }

    private int numErrors;
    private int numWarnings;
    private branchcond theBranches;
}
